package com.riku.security.controller;

import com.riku.security.common.SeCommonResponse;
import com.riku.security.model.SeRoleUser;
import com.riku.security.model.SeUser;
import com.riku.security.service.SeUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 1 on 2021/5/18.
 */
@Api(value = "security-user",description = "权限-用户")
@RestController
@RequestMapping("/se/user/")
public class SeUserController {

    @Autowired
    private SeUserService seUserService;

    @ApiOperation("新增用户")
    @PostMapping("insertUser")
    public SeCommonResponse insertUser(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) String userId,
            @ApiParam(required = true) @RequestParam(required = true) String userName,
            @ApiParam(required = false) @RequestBody(required = false) List<Long> roleIds
    ) {
        seUserService.insertUser(serviceCode,userId,userName,roleIds);
        return SeCommonResponse.success();
    }

    @ApiOperation("修改用户")
    @PutMapping("updateUser")
    public SeCommonResponse updateUser(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) String userId,
            @ApiParam(required = true) @RequestParam(required = true) String userName,
            @ApiParam(required = false) @RequestBody(required = false) List<Long> roleIds
    ) {
        seUserService.updateUser(serviceCode,userId,userName,roleIds);
        return SeCommonResponse.success();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("deleteUser")
    public SeCommonResponse deleteUser(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) String userId
    ) {
        seUserService.deleteUser(serviceCode,userId);
        return SeCommonResponse.success();
    }

    @ApiOperation("用户列表")
    @GetMapping("selectUserPage")
    public SeCommonResponse<List<SeRoleUser>> selectUserPage(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = false) @RequestParam(required = false) String userName
    ) {
        return SeCommonResponse.success(seUserService.selectByName(serviceCode,userName));
    }

    @ApiOperation("用户权限详情")
    @GetMapping("selectUserDetail")
    public SeCommonResponse<List<SeRoleUser>> selectUserDetail(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) String userId
    ) {
        return SeCommonResponse.success(seUserService.selectUserDetail(serviceCode,userId));
    }

    @ApiOperation("删除用户角色权限")
    @DeleteMapping("deleteRoleUser")
    public SeCommonResponse deleteRoleUser(
            @ApiParam(required = true) @RequestParam(required = true) Long id
    ) {
        seUserService.deleteRoleUser(id);
        return SeCommonResponse.success();
    }
}
